/*==========================================================
 *
 * @author	Limpo, limpo_soft@qq.com
 * @date	10/30/11 12:30:36 CST
 * @brief	buffer pool
 *
 *  Copyright (c) 2011, Limpo
 *========================================================== 
 */

#ifndef _OXYGEN_BUFFER_H_
#define _OXYGEN_BUFFER_H_

#include <vector>
#include "common.h"

#define INIT_SIZE (1024 * 30)

namespace Oxygen
{
	class Buffer
	{
		typedef std::vector< char > BufferPool;
	public:
		Buffer( uint32 size = INIT_SIZE );
		virtual ~Buffer();

		int Push( const char *data, int len );
		int Fetch( char *data, int offset = 0 );

		uint Total() const;
		uint Space() const;
		uint Length() const;
		bool Empty() const;
		void Clear();

		void DebugString() const;

	private:
		bool Dectet( int len );

	private:
		BufferPool 	m_pool;
		uint32		m_size;
	};

	// inline of Buffer
	inline uint Buffer::Total() const
	{
		return m_pool.capacity();
	}

	inline uint Buffer::Space() const
	{
		return m_pool.capacity() - m_size;
	}

	inline uint Buffer::Length() const
	{
		return m_size;
	}

	inline bool Buffer::Empty() const
	{
		return m_size == 0;
	}
	
	inline void Buffer::Clear()
	{
		m_pool.clear();
		m_size = 0;
	}

}; // end of namespace
#endif
